//
//  BLConsumeViewController.m
//  blanket
//
//  Created by Mac on 2017/12/7.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLConsumeViewController.h"
#import "BLConsumeTableViewCell.h"
#import "BLXiaofeiModel.h"
@interface BLConsumeViewController ()
@property(nonatomic, strong) NSMutableArray                    *dataSouce;
@property(nonatomic, strong) STNoresultView                     *noresultView;
@property(nonatomic, assign) NSInteger                     page;
@property(nonatomic, strong) STSegementButton                     *segementButton;
@end

@implementation BLConsumeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消费记录";
    self.automaticallyAdjustsScrollViewInsets = NO;
    __weak typeof(self) weakSelf =  self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, [TMUtils navigationBar].bottom, UIScreenWidth, 44)
                                                         andTitle:@[@"现金",@"盖币",@"积分"] handle:^(STSegementButton *sender) {
                                                             weakSelf.page = 1;
                                                             [weakSelf.tableView.mj_header beginRefreshing];
                                                         }];
    
    self.segementButton.backgroundColor = [UIColor whiteColor];
    self.segementButton.lineSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleSelectedColor = KL_BlueBackGroundColor;
    self.segementButton.butTitleColor = KL_secendTextColor;
    self.segementButton.lineColor = [UIColor whiteColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    [self.view addSubview:self.segementButton];
    
    self.tableView.frame = CGRectMake(0, self.segementButton.bottom, UIScreenWidth, [TMUtils tabbar].top + 49 - self.segementButton.bottom);
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self sendCustomListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self sendCustomListRequest];
    }];
    //self.tableView.mj_footer.hidden = YES;
    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 180, UIScreenWidth, 300)
                                                        title:@"暂无消费"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noresultView.hidden = YES;
    [self.tableView addSubview:self.noresultView];
    // Do any additional setup after loading the view.
}
- (void)reloadData{
    self.noresultView.hidden = self.dataSouce.count;
//    if (self.dataSouce.count > 4) {
//        self.tableView.mj_footer.hidden = NO;
//    }else{
//        self.tableView.mj_footer.hidden = YES;
//    }
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BLConsumeTableViewCell CellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLConsumeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLConsumeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)sendCustomListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/consumeAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [dic setObject:@(self.page) forKey:@"page"];
    
    NSInteger index = self.segementButton.cureentIndex;
    [dic setObject:@(index+1) forKey:@"type"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_footer endRefreshing];
                                                 if (self.page == 1) {
                                                     self.dataSouce = [NSMutableArray new];
                                                 }
                                                 NSArray * dataArray = responseObject[@"data"];
                                                 for (NSDictionary * modelDic in dataArray) {
                                                     BLXiaofeiModel * mdoel = [BLXiaofeiModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:mdoel];
                                                 }
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                                 [SVProgressHUD dismiss];
                                                 [self reloadData];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}


@end
